<?php
/**
 *  载入 app database template 对象！
 * 
 */

abstract class AbstructApp{
	protected  $db;
	protected  $tpl;
	protected static $log;
	protected $template;//应用程序调用的模板
	protected $class;//应用对象-响应模块
	protected $method;//响应方法
	
	protected $appName;//应用名称 \  - 主要用于记录日志使用
	protected $modName;//方法名称 /	
	abstract protected  function todo();
	abstract protected  function def();
	
	public function setTemplate($template){
		$this->template = $template;	
	}
	
	public function setClass($param){
		$this->class = $param;
	}
	
	public function setMethod($param){
		$this->method = $param;
	}
	
	public function getModName(){
		return $this->modName;
	}
	
	public function setAppName($param){
		$this->appName = $param;
	}
	public function setModName($param){
		$this->modName = $param;
	}
	
	public function setDb($param){
		$this->db = $param;
	}
	
	public function setTpl($param){
		$this->tpl = $param;
	}
	
	//将应用需要的对象载入
	public function __construct(){
	}

	
	//记录日志信息 (应用程序库日志信息)
	public function __destruct(){
	}
}

/**
 * 数据库对象抽象对象
 *
 *  所提供的数据库对象必须实现以下接口
 */

abstract class AbstructDatabase{
	protected static $log;
	abstract protected  function getRow($sql,$condition=array());
	abstract protected  function getAll($sql,$condition=array());
	abstract protected  function Execute($sql,$condition=array());
	abstract protected  function lastInsertId();
	abstract protected  function connect($dbhost,$user,$pass,$db,$defaultcharset);
	abstract protected  function disconnect($handle=NULL);
	abstract protected  function getPage($sql,$condition,$pagesize=1);
	
	abstract protected  function insert($table,$fields,&$insertid=-1);
	abstract protected  function select($table,$fields,$condition=array());
	abstract protected  function delete($table,$condition=array());
	abstract protected  function update($table,$fields,$condition=array());
	
	abstract protected  function getCount($table,$condition=array());//统计
	
	abstract protected  function testSql($sql);
	
	
	//记录SQL日志信息 (SQL 操作日志信息)
	public function __destruct(){
	}
}


/**
 * 模板对象实现抽象定义
 *
 * 模板对象必须实现以下定义内容
 * 
 */
abstract class Abstructtemplate{
	protected static $log;
	abstract protected function assign($key,$value);	//向模板内注入数据
	abstract protected function display($template);	//显示模板内容
	abstract protected function test($template);	//测试模块
	
	//记录日志信息 (模板级日志信息)
	public function __destruct(){
	}
}


class _Adt{
	private $database;
	private $template;
	private $application;
	
	public function __construct(){
		_importClass("database");
		_importClass("template");
		$this->database = new database();
		$this->database->connect(_cfg::dbHost,_cfg::dbUser,_cfg::dbPass ,_cfg::dbName ,_cfg::dbChar);
		$this->template = new template();
	}
	
	/**
	 * 注入应用模块
	 *
	 * @param Application $app
	 */
	public function setApp(&$app){
		if('object'!=gettype($app))return;
		$this->application = $app;
	}
	
	public function getApp(){
		return $this->application;
	}
	
	public function run(){
		//开始执行显示内容信息！！
		$this->application->setDb(&$this->database);
		$this->application->setTpl(&$this->template);
		$method = $this->application->getModName();
		_debug(array(__FILE__,__LINE__,"Call ".get_class($this->application)." $method !"));
		if(method_exists($this->application,$method))$this->application->$method();
//		try{
//			$this->application->$method();
//		}
//		catch (Exception $e){
//			throw new Exception(_ExceptionString("$method not found!"));
//		}
	}
}


return md5(__FILE__);